package turtle.system.doug.turtleaction;

import java.io.IOException;
import java.util.ArrayList;

import turtle.system.pt.Bar;
import turtle.system.pt.BarSet;
import turtle.system.pt.StatCalc;
import turtle.system.util.Action;

public class TurtleAction extends Action{		
	
	public static ArrayList<TurtleAction> allActions= new ArrayList<TurtleAction>();
	public static int daysIn4Long;
	public static int daysOut4Long;
	public static int daysIn4Short;
	public static int daysOut4Short;
	public static int stopProfit4Long;
	public static int stopProfit4Short;
	public static int stopLoss4Long;
	public static int stopLoss4Short;
	
	public static int FORCE_OUT_VALUE;
	public static int HALF_FORCE_OUT_VALUE;
	
	
    public double force_out_price;
    Bar todayBar;
    Bar todayBar2;
    Bar tomorrowBar;
    Bar tomorrowBar2;
    private int addInTime;       
    private int addOutTime;
    private int addLongOrShort;
    int todayIndex;
    public int tradeId = 0;
    
    public TurtleAction(BarSet barset, BarSet barset2, int todayIndex) {
      this.todayIndex = todayIndex;
      todayBar = barset.getBar(todayIndex);
      todayBar2 = barset2.getBar(todayIndex);
      tomorrowBar = barset.getBar(todayIndex+1);
      tomorrowBar2 = barset2.getBar(todayIndex+1);
      allActions.add(this);
      this.tradeId = allActions.size();
    }
    public TurtleAction(Bar todayBar2, Bar tomorrowBar2) {
        this.todayIndex = -1;
        this.todayBar = null;        
        todayBar2 = todayBar2;
        tomorrowBar = null;
        tomorrowBar2 = tomorrowBar2;                
      }
    
    public void setData(int inOrOut, int longOrShort, int reason, int actTime, double price) {
    	this.inOrOut = inOrOut;
    	this.longOrShort = longOrShort;
    	this.reason = reason;
    	this.outAtStartOrEnd = actTime;
    	this.price = price;
    }

    public String getActionMessage() {
    	StringBuffer buf = new StringBuffer();
    					
		if (this.inOrOut == IN_ACTION) {
			buf.append(tomorrowBar.mergeBar(tomorrowBar2) + "\n");
			buf.append("開盤進場"+ ", 理由："+"前一日創 " + (
					(this.longOrShort == LONG_ACTION || this.longOrShort == LONG_ACTION_KEEP_PREV_TRADE_OR_ADVANCED_IN) ? 
					(daysIn4Long +" 天新高"): (daysIn4Short +" 天新低")) 
					+  ", ");
			buf.append(String.format("前一日大盤[%.0f-%.0f-%.0f-%.0f-diff=%.0f], ",
					todayBar.open, todayBar.high, todayBar.low, todayBar.close,
					todayBar.diff));
			buf.append("做" + ((this.longOrShort == LONG_ACTION || this.longOrShort == LONG_ACTION_KEEP_PREV_TRADE_OR_ADVANCED_IN) ?
					"多" + ((this.longOrShort == LONG_ACTION_KEEP_PREV_TRADE_OR_ADVANCED_IN ? "(昨單續留或前提進場)" : "") + ", 進場價 "
					+ String.format("%.0f", this.price) + " (停損價:"
					+ String.format("%.0f", this.price - FORCE_OUT_VALUE) + ")") 
					
					: "空"+ ((this.longOrShort == SHORT_ACTION_KEEP_PREV_TRADE_OR_ADVANCED_IN ? "(昨單續留或前提進場)" : "") + ", 進場價 "
							+ String.format("%.0f", this.price) + " (停損價:"
							+ String.format("%.0f", this.price + FORCE_OUT_VALUE) + ")")							
					));								
		}
		else {						
			if (this.reason == FORCE_STOP_LOSS_REASON) {
				buf.append(tomorrowBar.mergeBar(tomorrowBar2) + "\n");
				buf.append("強制出場, 出場價 " + String.format("%.0f", this.price)
						+ ", 理由：" + "盤中強制停損" + FORCE_OUT_VALUE + "出場,");
			}
			else if (this.reason == FORCE_STOP_LOSS_AT_START_REASON) {
				buf.append(tomorrowBar.mergeBar(tomorrowBar2) + "\n");
				buf.append("強制出場, 出場價 " + String.format("%.0f", this.price)
					+ ", 理由：" + "盤中開盤強制停損" + FORCE_OUT_VALUE + "出場,");			
			}
			else if (this.reason == FORCE_STOP_LOSS_TODAY_REASON) {
				buf.append(todayBar.mergeBar(todayBar2) + "\n");
				buf.append("強制出場, 出場價 " + String.format("%.0f", this.price)
						+ ", 理由：" + "盤中強制停損" + FORCE_OUT_VALUE + "出場,");				
			}
			else {
				buf.append(tomorrowBar.mergeBar(tomorrowBar2) + "\n");
				switch (this.outAtStartOrEnd) {
				case OUT_ACTION_AT_START:
					buf.append("開盤出場");
					break;
				case OUT_ACTION_AT_END:
					buf.append("收盤出場");
					break;
                default:
                	assert(true); /*currently not support*/
                	break;
				}
				
				buf.append((", 出場, 出場價 " + String.format("%.0f", this.price))+", 理由：");
				
				switch (this.reason) {
				case TREND_REVERT_REASON:
					buf.append((this.longOrShort == LONG_ACTION ? ("前一日創 "+ daysOut4Long +" 天新低"):("前一日創 "+daysOut4Short +" 天新高"))+", ");
					break;
				case STOP_LOSS_REASON:
					buf.append((this.longOrShort == LONG_ACTION ? ("多單停損 "+ stopLoss4Long +" 點    "):("空單停損"+stopLoss4Short +" 點    "))+", ");					
					break;
				case STOP_PROFIT_REASON:
					buf.append((this.longOrShort == LONG_ACTION ? ("多單停利 "+ stopProfit4Long +" 點    "):("空單停利"+stopProfit4Short +" 點    "))+", ");										
					break;					
                default:
                	assert(true); /*currently not support*/
                	break;
				}		
				buf.append(String.format("前一日大盤[%.0f-%.0f-%.0f-%.0f-diff=%.0f], ",
						todayBar.open, todayBar.high, todayBar.low, todayBar.close,
						todayBar.diff));	
			}
				
				double prevPrice = allActions.get(allActions.size()-2).price;				
				buf.append(" (停損價設定:"						
						+ String.format("%.0f", (this.longOrShort == LONG_ACTION ?  
								prevPrice - FORCE_OUT_VALUE : prevPrice + FORCE_OUT_VALUE)) 
								+ "),");

				double profit = this.longOrShort == LONG_ACTION ? price - prevPrice : prevPrice - price; 				
				buf
				.append(" 獲利 * * * ["
						+ String.format("%.0f]", profit));
		}   	
		System.out.println(buf.toString());
		System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxx");
    	return buf.toString();
    }

}
